package com.weilan.blog.picture.controller;

import com.weilan.blog.base.exception.ThrowableUtils;
import com.weilan.blog.base.validator.group.Delete;
import com.weilan.blog.base.validator.group.GetList;
import com.weilan.blog.base.validator.group.Insert;
import com.weilan.blog.base.validator.group.Update;
import com.weilan.blog.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.weilan.blog.common.global.SysConf;
import com.weilan.blog.picture.entity.PictureCategory;
import com.weilan.blog.picture.service.PictureCategoryService;
import com.weilan.blog.picture.vo.PictureCategoryVO;
import com.weilan.blog.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/10/10 15:35
 */
@Slf4j
@RestController
@RequestMapping("/pictureCategory")
public class PictureCategoryController {

    @Autowired
    private PictureCategoryService pictureCategoryService;

    @PostMapping(value = "/getList")
    public String getList(@Validated({GetList.class}) @RequestBody PictureCategoryVO pictureCategoryVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.result(SysConf.SUCCESS, pictureCategoryService.getPageList(pictureCategoryVO));
    }

    /**
     * 新增分类
     *
     * @param pictureCategoryVO
     * @param result
     * @return
     */
    @AvoidRepeatableCommit
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody PictureCategoryVO pictureCategoryVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return pictureCategoryService.addPictureCategory(pictureCategoryVO);
    }

    /**
     * 编辑分类
     *
     * @param pictureCategoryVO
     * @param result
     * @return
     */
    @PutMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody PictureCategoryVO pictureCategoryVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return pictureCategoryService.editPictureCategory(pictureCategoryVO);
    }

    /**
     * 根据分类id删除分类
     *
     * @param pictureCategoryUid
     * @return
     */
    @DeleteMapping("/{pictureCategoryUid}")
    public String delete(@PathVariable("pictureCategoryUid") String pictureCategoryUid) {

        return pictureCategoryService.deletePictureCategory(pictureCategoryUid);
    }

    /**
     * 根据id获取分类信息
     *
     * @param pictureCategoryUid
     * @return
     */
    @GetMapping("/{pictureCategoryUid}")
    public String getPictureCategoryByUid(@PathVariable("pictureCategoryUid") String pictureCategoryUid) {
        PictureCategory pictureCategory = pictureCategoryService.getById(pictureCategoryUid);
        return ResultUtil.result(SysConf.SUCCESS, pictureCategory);
    }

    /**
     * 根据id置顶图片分类
     *
     * @param pictureCategoryUid
     * @return
     */
    @PutMapping("/stick/{pictureCategoryUid}")
    public String stick(@PathVariable("pictureCategoryUid") String pictureCategoryUid) {

        return pictureCategoryService.stickPictureCategory(pictureCategoryUid);
    }
}
